package com.younion.action.manage;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.younion.business.IMemberCardCostCountService;
import com.younion.common.base.BaseAction;
import com.younion.common.constants.TemplateTargetType;
import com.younion.common.exception.BusinessException;
import com.younion.common.paginator.Paginator;
import com.younion.permission.PermissionUtil;
import com.younion.util.YounionUtil;
import com.younion.vo.CostVO;
import com.younion.vo.MemberCardCostCountVO;
import com.younion.vo.MemberCardVO;
import com.younion.vo.MembercardServiceVO;


/**
 * @author Joe Chen
*/
public class MemberCardCostCountAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	protected static final Log logger = LogFactory.getLog(MemberCardCostCountAction.class);

	@Autowired
	private IMemberCardCostCountService memberCardCostCountService = null;

	private MemberCardCostCountVO memberCardCostCountVO = new MemberCardCostCountVO();

	private Collection memberCardCostCountList;// 结果集列表
	
	private CostVO costVO = new CostVO();
	
	/**
	 * 列表：翻页区；数据区
	 */
	@SuppressWarnings("unchecked")
	public String listMemberCardCostCount() {
		try {
			memberCardCostCountVO.setMemberCard(new MemberCardVO(costVO.getCardEntity().getId()));
			Map paramMap = Maps.newHashMap();
			paramMap.put("memberCardId", costVO.getCardEntity().getId());
			// 获取总记录数
			//int total = memberCardCostCountService.getCountByMap(paramMap);
			// 根据总记录数获取分页对象
			//Paginator paginator = this.getPaginator(total);
			memberCardCostCountList = memberCardCostCountService.getByMap(paramMap);
			// 保存分页对象
			//this.savaPaginator(paginator);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String listMemberCardCostCountView() {
		try {
			Map paramMap = Maps.newHashMap();
			paramMap.put("memberCardId", costVO.getMemberCardId());
			if (costVO.getProductName() != null && !"".equals(costVO.getProductName()))
				paramMap.put("productName", costVO.getProductName());
			// 获取总记录数
			int total = memberCardCostCountService.getCountByMap(paramMap);
			// 根据总记录数获取分页对象
			Paginator paginator = this.getPaginator(total);
			memberCardCostCountList = memberCardCostCountService.getByMap(paramMap);
			// 保存分页对象
			this.savaPaginator(paginator);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return SUCCESS;
	}

	/**
	 * 新增
	 */
	public String addMemberCardCostCount() {
		return SUCCESS;
	}

	/**
	 * 修改保存，调度Jsp
	 */
	public String editMemberCardCostCount() {
		try {
			memberCardCostCountVO = memberCardCostCountService.getById(memberCardCostCountVO.getId().toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 保存动作
	 */
	public String saveMemberCardCostCount() {
		try {
			memberCardCostCountVO = memberCardCostCountService.save(memberCardCostCountVO);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 删除
	 */
	public String deleteMemberCardCostCount() {
		try {
			String pkId = request.getParameter("key");
			memberCardCostCountService.remove(Arrays.asList(pkId.split(",")));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;
	}
	/**
	 * 查看鿴
	 */
	public String viewMemberCardCostCount() {
		try {
			String pkId = request.getParameter("key");  
			memberCardCostCountVO = memberCardCostCountService.getById(pkId);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;

	}
	
	public String costCount(){
		try{
			String pkId = request.getParameter("key");
			String count = request.getParameter("count");
			String memberCardId = request.getParameter("membercardId");
			String customerId = request.getParameter("customerId");
			List<Integer> ids = Lists.newArrayList();
			for(String id : pkId.split(PermissionUtil.comma)){
				ids.add(Integer.valueOf(id));
			}
			List<MemberCardCostCountVO> costCounts = memberCardCostCountService.selectByIds(ids);
			memberCardCostCountService.updateCostCount(getSessionUser(),costCounts, Integer.valueOf(count), Integer.valueOf(memberCardId));
			for(MemberCardCostCountVO c : costCounts){
				MembercardServiceVO service = retreiveMemberCardService(c.getMemberCard(), TemplateTargetType.COSTCOUNT);
				if(service != null){
					String content = YounionUtil.generateMessageContent(service.getTemplate().getContent(), service.getMemberCard());
					content = YounionUtil.generateChangeData(content, null,Integer.valueOf(count)  , null, null, c.getCount(), null, null);
					if(c.getProduct() != null && c.getProduct().getName() != null){
						content = YounionUtil.generateMessageContentByProduct(content, c.getProduct().getName());
					}
					sendNotification(service,TemplateTargetType.COSTCOUNT, content);
				}
			}
			
			this.addLog(3, "计次消费：会员卡ID"+ memberCardId , "计次消费",getSessionUser());
			request.setAttribute("refreshNavTabId", "costList");
			request.setAttribute("callbackType", "forward");
			request.setAttribute("forwardUrl", request.getContextPath()+"/cost/listCost.action");
			return SUCCESS;
		}catch(Exception e){
			if(e instanceof BusinessException ){
				tipMessage = e.getMessage();
			}else{
				tipMessage = "扣次消费发生出错！";
				e.printStackTrace();
			}
			request.setAttribute("tipMessage", tipMessage);
			return ERROR;
		}
	}
	

	public MemberCardCostCountVO getMemberCardCostCountVO() {
		return memberCardCostCountVO;
	}

	public void setMemberCardCostCountVO(MemberCardCostCountVO memberCardCostCountVO) {
		this.memberCardCostCountVO = memberCardCostCountVO;
	}

	public void setMemberCardCostCountService(IMemberCardCostCountService memberCardCostCountService) {
		this.memberCardCostCountService = memberCardCostCountService;
	}

	public Collection getMemberCardCostCountList() {
		return memberCardCostCountList;
	}

	public void setMemberCardCostCountList(Collection memberCardCostCountList) {
		this.memberCardCostCountList = memberCardCostCountList;
	}

	public IMemberCardCostCountService getMemberCardCostCountService() {
		return memberCardCostCountService;
	}

	public CostVO getCostVO() {
		return costVO;
	}

	public void setCostVO(CostVO costVO) {
		this.costVO = costVO;
	}
	
	
}